package com.xul.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xul.yygh.cmn.client.DictFeignClient;
import com.xul.yygh.hosp.repository.HospitalRepository;
import com.xul.yygh.hosp.service.HospitalService;

import model.com.xul.yygh.model.hosp.Hospital;

import model.com.xul.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-10 22:06
 */

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院接口
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        // 把map对象转换成Hospital对象
        String toJSONString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(toJSONString, Hospital.class);
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        // 判断是否存在数据
        if (hospitalExist != null) {
            // 存在数据就行修改
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            // 不存在数据经行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    /**
     * 获取医院的信息
     *
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * 医院列表分页接口
     *
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> findPageHospital(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 创建条件构造器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> hospitals = hospitalRepository.findAll(example, pageable);
        // 获取的list的集合,遍历经行医院等级的封装
        hospitals.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return hospitals;
    }

    /**
     * 更新上线状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        // 根据id查询到医院的信息
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 医院详情信息
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getHospById(String id) {
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        Map<String, Object> map = new HashMap<>();
        // 放到map中医院的基本信息
        map.put("hospital", hospital);
        // 放入预约信息
        map.put("bookingRule", hospital.getBookingRule());
        // 不需要重复传入
        hospital.setBookingRule(null);
        return map;
    }

    /**
     * 根据hoscode获取到医院的名称
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据医院名称经行模糊查询
     *
     * @param hosname
     * @return
     */
    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 根据医院编号获取医院预约详情
     *
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        // 获取医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        Map<String, Object> map = new HashMap<>();
        map.put("hospital", hospital);
        // 预约规则
        map.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }


    private Hospital setHospitalHosType(Hospital hospital) {
        // 根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        // 查询省 市 地区 信息
        String provinceCodeString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityCodeString = dictFeignClient.getName(hospital.getCityCode());
        String districtCodeString = dictFeignClient.getName(hospital.getDistrictCode());
        // 将查询的数据封装到Hospital对象中的param中
        hospital.getParam().put("fullAddress", provinceCodeString + cityCodeString + districtCodeString);
        hospital.getParam().put("hostypeString", hostypeString);
        return hospital;
    }


}
